package com.ccb.afm.util;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ccb.modelbean.Materials;

import cn.tworen.demou.remoting.AmfMessage;

public class PriceTool {

	public float steel;//钢材
	public float decorative;//装饰
	public float cement;//水泥
	public HttpClient client = new HttpClient();
	private static Log log = LogFactory.getLog(PriceTool.class); //日志
	public CodingFactory cfy=new CodingFactory();
	public PriceTool()
	{
		//初始化代理信息
		
		
		 HostConfiguration cf   =     new   HostConfiguration();
		 cf.setHost("baiduhouse.17c.cn");
		 if(ConfigData.isproxy){
		 cf.setProxy( ConfigData.proxyip  ,   ConfigData.proxyport  );
		 }
		 
		 client.setHostConfiguration(cf);
		 
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		PriceTool p=new PriceTool();
		//p.getprice("6bc8e821f598a7c84151017bfc5091b7");
		p.getMaterial("6bc8e821f598a7c84151017bfc5091b7");

	}
	
	
	
	/**
	 * 设置amf数据 http头部
	 */
	public void setamfHead(PostMethod method)
	{
		 method.setPath("/apf_houseking/baidu_stable/index.php?c=api&a=gateway");
		 method.setRequestHeader("Accept-Encoding","gzip,deflate");
		 method.setRequestHeader("Host","baiduhouse.17c.cn");
		 method.setRequestHeader("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8");
		 method.setRequestHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		 method.setRequestHeader("Accept-Language","en-us,zh-cn;q=0.7,zh;q=0.3");
		 method.setRequestHeader("Accept-Charset","GB2312,utf-8;q=0.7,*;q=0.7");
		 method.setRequestHeader("Keep-Alive","115");
		 method.setRequestHeader("Connection","keep-alive");
		 method.setRequestHeader("Referer","http://swf4.17c.cn/hkswf/swf_20100802/Main001.swf");
		 method.setRequestHeader("Content-type","application/x-amf");
		
	}
	
	/**
	 * 获得价钱
	 * @param housekingcookie
	 */
	public void getprice(String housekingcookie)
	{    
		 
		
		 PostMethod method = new PostMethod();
		 //设置头部
		 setamfHead(method);
		 //method.setRequestHeader("Content-length","100");
		 
		 
		 try {
				//method.setRequestBody(new FileInputStream("c:/input.log"));
			    ByteArrayInputStream   b=new ByteArrayInputStream(cfy.getInputStream(housekingcookie,"c:/input.log")); 
			    method.setRequestBody(b);
				int status=client.executeMethod(method);
				AmfMessage message= CodingFactory.decodezipmsg(method.getResponseBodyAsStream()); 
			    HashMap body=(HashMap)message.getBodies().get(0).getValue();
			    decorative=getoneprice("decorative",body);
			    steel=getoneprice("steel", body);
			    cement=getoneprice("cement", body);
			    System.out.println(cement+":"+decorative +":"+steel);
			    //System.out.println(body);
				
		
			}catch (Exception e) {
				e.printStackTrace();
			}
			}
	
	/**
	 * 获得现有材料数量
	 */
	
	public void getMaterial(String housekingcookie)
	{    
		 
		
		 PostMethod method = new PostMethod();
		 //设置头部
		 setamfHead(method);
		 //method.setRequestHeader("Content-length","100");
		 
		 
		 try {
				//method.setRequestBody(new FileInputStream("c:/input.log"));
			    ByteArrayInputStream   b=new ByteArrayInputStream(cfy.getInputStream(housekingcookie,"c:/buymaterials.log",89)); 
			    method.setRequestBody(b);
				int status=client.executeMethod(method);
				AmfMessage message= CodingFactory.decodezipmsg(method.getResponseBodyAsStream()); 
				Materials materials=Materials.getMaterials(message);
			   
				System.out.println("");
				
		
			}catch (Exception e) {
				e.printStackTrace();
			}
			}
	
	
	
	
	/**
	 * 从Hash表中获得返回数据
	 * @param name
	 * @param body
	 * @return
	 */
	public float getoneprice(String name,HashMap body)
	{
	float num=0.0f;
	Object[] m=(Object[])body.get(name);
	Object[] mm=(Object[])m[6];
	String  str=(String)mm[0];
	num=Float.parseFloat(str);
	return num;
	}
	
	
	
	
	
	
	
}


